package com.superyee.commonlib.http;

import android.content.Context;
import android.text.TextUtils;

import com.superyee.commonlib.constants.StaticConstant;
import com.superyee.commonlib.utils.SpUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class HeaderParamsInterceptor implements Interceptor {

    private Context mContext;

    public HeaderParamsInterceptor(Context context) {
        this.mContext = context.getApplicationContext();
    }

    private HeaderParamsInterceptor() {

    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        String token = SpUtils.getString(mContext, StaticConstant.SP.ACCESS_TOKEN, null);
        Request.Builder builder = originalRequest.newBuilder()
                .removeHeader("Accept")
                .addHeader("Accept", "application/vnd.zhb.v1+json");
        if (!TextUtils.isEmpty(token)) {
            builder.header(StaticConstant.SP.AUTHORIZATION, "Bearer " + token);
        }
        Request newRequest = builder.build();
        Response response = chain.proceed(newRequest);
        return response;
    }
}
